//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchType: 0,
    recentlySearchData: [],
    hotSearchData: ['洗衣液', '牙膏', '指纹锁', '净水器', '蛋糕'],
    // 店铺或商家
    selectPerson: true,
    firstPerson: '商品',
    selectArea: false,
    // 搜索
    searchWhat: "",
    searchGoodsOrStore: "商品",
    searchId: 0,
    // 
    uid: ""
  },

  /**
   * 生命周期函数--监听页面加载    
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetMySearchRecord();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var recentlySearchData = wx.getStorageSync('recentlySearchData');
    if (recentlySearchData) {
      //如果有搜索历史的本地
      this.setData({
        recentlySearchData: recentlySearchData
      })
    } else {
      //没有搜索历史时      
      wx.setStorageSync('recentlySearchData', []);
    }
  },

 
  // 搜索历史记录
  GetMySearchRecord: function () {
    var that = this;
    var uid = this.data.uid;
    var searchType = this.data.searchType;
    var recentlySearchData = this.data.recentlySearchData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMySearchRecord&UID=" + uid + "&sType=" + searchType),
      success: function (res) {
        recentlySearchData = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        that.setData({
          recentlySearchData: recentlySearchData,
        })
        console.log(recentlySearchData);
      }
    })
  },
  //点击选择商家或店铺
  clickPerson: function () {
    var selectPerson = this.data.selectPerson;
    if (selectPerson == true) {
      this.setData({
        selectArea: true,
        selectPerson: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectPerson: true,
      })
    }
  },
  mySelect: function (e) {
    console.log(e)
    var searchId = this.data.searchId;
    var searchGoodsOrStore = this.data.searchGoodsOrStore;
    this.setData({
      firstPerson: e.target.dataset.me,
      selectPerson: true,
      selectArea: false,
      searchId: e.target.dataset.typeid,
      searchGoodsOrStore: e.target.dataset.me,
    })

  },
  // 搜索输入框
  inputTap: function (e) {
    var searchWhat = this.data.searchWhat;
    this.setData({
      searchWhat: e.detail.value,
    })
    console.log(e)
  },

  // 热门搜索
  searchWhatTap: function (e) {
    console.log(e);
    var searchWhat = this.data.searchWhat;
    this.setData({
      searchWhat: e.currentTarget.dataset.value,
    })
  },

  // 清除历史
  clearWhatTap: function (e) {
    console.log(e);
    var searchWhat = this.data.searchWhat;
    this.setData({
      searchWhat: e.currentTarget.dataset.value,
    })
    wx.setStorageSync('recentlySearchData', []);
    // wx.clearStorageSync('recentlySearchData');
    this.setData({
      recentlySearchData: []
    })
  },
  // 历史搜索
  recentlyWhatTap: function (e) {
    console.log(e);
    var searchWhat = this.data.searchWhat;
    this.setData({
      searchWhat: e.currentTarget.dataset.value,
    })
  },

  // 点击搜索图标
  goSearch: function () {
    // if (app.isLogin()) {
      var searchId = this.data.searchId;
      var searchGoodsOrStore = this.data.searchGoodsOrStore;
      var searchWhat = this.data.searchWhat;
      wx.navigateTo({
        url: '/packageA/searchResult/searchResult?&searchGoodsOrStore=' + searchGoodsOrStore + "&searchId=" + searchId + "&searchWhat=" + searchWhat,

      })
    // }
  },
  // 
})